import { fixappLogin } from "@/api/login";
import { User } from "@/utils/cache";
export const SET_USERINFO = "SET_USERINFO";
export const LOGIN = "LOGIN";

const user = {
  state: {
    userInfo: User.getUserInfo(),
  },
  mutations: {
    [SET_USERINFO]: (state, payload) => {
      state.userInfo = payload;
    },
  },

  actions: {
    [LOGIN]({ commit }, payload) {
      return new Promise((resolve, reject) => {
        fixappLogin(payload)
          .then((res) => {
            commit(SET_USERINFO, res.data);
            User.setUserInfo(res.data);
            resolve();
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
  },
};

export default user;
